package com.heima.comment.service.impl;

import com.heima.comment.pojos.ApComment;
import com.heima.comment.pojos.ApCommentLike;
import com.heima.comment.service.ApCommentLikeService;
import com.heima.model.comment.dtos.CommentLikeDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApUserThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: Zqr
 */
@Slf4j
@Service
public class ApCommentLikeServiceImpl implements ApCommentLikeService {
    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 评论点赞
     * 数据保存到MongoDB中
     *
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public ResponseResult commentLike(CommentLikeDto dto) {
        //1.入参校验
        if (dto.getCommentId() == null || dto == null || (dto.getOperation() != 0 && dto.getOperation() != 1)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否登录
        ApUser apUser = ApUserThreadLocalUtil.getUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //根据评论的id查询该评论详情
        ApComment apComment =
                mongoTemplate.findOne(Query.query(Criteria.where("id").is(dto.getCommentId().toString())), ApComment.class);

        //2.根据operation 对应的更新MongoDB中对应的数据     0：点赞       1：取消点赞
        if (dto.getOperation() == 0) {
            //判断用户是否已经点赞
            ApCommentLike dbApCommentLike = mongoTemplate.findOne(Query.query(Criteria.where("commentId").is(dto.getCommentId().toString())
                    .and("authorId").is(apUser.getId())), ApCommentLike.class);
            if (dbApCommentLike != null) {
                //return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "当前用户已点赞,无法继续点赞!");
                apComment.setLikes(1);//评论点赞数+1
            } else {
                //点赞 插入一条数据到ap_comment_like ,同时更新ap_comment中 该条评论对应的likes点赞数+1
                ApCommentLike apCommentLike = new ApCommentLike();
                apCommentLike.setAuthorId(apUser.getId());
                apCommentLike.setCommentId(dto.getCommentId());

                mongoTemplate.save(apCommentLike, "ap_comment_like");

                apComment.setLikes(apComment.getLikes() + 1);//评论点赞数+1
            }

        } else {
            //取消点赞,删除MongoDB中该条数据 ,同时更新ap_comment中 该条评论对应的likes点赞数-1
            mongoTemplate.remove(Query.query(Criteria.where("authorId").is(apUser.getId())
                    .and("commentId").is(dto.getCommentId())), "ap_comment_like");

            //更新该评论的点赞数
            int temp = apComment.getLikes() - 1;  //
            if (temp < 0) {
                apComment.setLikes(0);
            } else {
                apComment.setLikes(apComment.getLikes() - 1);
            }
        }

        mongoTemplate.save(apComment); //更新评论详情

        //3.响应数据: 当前评论的点赞数
        Map resMap = new HashMap();
        resMap.put("likes", apComment.getLikes());
        return ResponseResult.okResult(resMap);
    }
}
